import os
import shutil


def run():
    # target 目录
    base_dir = os.path.dirname(__file__)
    target_dir = base_dir + r"\target"

    # 匹配唯一jar包
    try:
        list_file = os.listdir(target_dir)
    except:
        print("target目录未找到")
        return
    file_name = ""  # jar包的名字
    for file in list_file:
        if file.endswith(".jar"):
            file_name = file
            break
    if file_name == "":
        print("jar包未找到")
        return
    else:
        print("jar包已找到，开始创建package文件夹")

    # 创建package文件夹
    package_dir = target_dir + r"\package"
    if not os.path.exists(package_dir):
        os.mkdir(package_dir)

    # 将匹配到的jar包放进package
    try:
        shutil.copyfile(target_dir + rf"\{file_name}", package_dir + rf"\{file_name}")
    except:
        print("jar包复制错误")
        return

    # 执行指令
    # jpackage -i package -n share --type "msi" --app-version 1.02 --verbose
    # --win-per-user-install --win-dir-chooser --win-shortcut --main-jar 匹配到的jar包.jar
    os.system(
        rf'cd target && jpackage -i package -n share --type "msi" --app-version 1.02 --verbose --win-per-user-install --win-dir-chooser --win-shortcut --main-jar {file_name}')

    print("脚本结束")


if __name__ == '__main__':
    run()
